// olive.js
// My collection of useful JS stuff
// Tested on jQuery >= 1.2.6

// Debugging
function alertVar(name, o) {
	alert(name + " = " + o);
}

// ----------------------------------
// HTML builder functions
XHTML = {
	button: function (cmd) {
		return $.create("span", {"class": "button", "onclick": cmd.command, "title": cmd.title}, [cmd.label]);
	}
}

// ----------------------------------
// Improve the array object

Array.prototype.random = function () { return this[Math.ran(0, this.length)]; }
Array.prototype.first = function() { return this[0]; }
Array.prototype.rest = function() { return this.slice(1); }

// Build the jQuery $.each(), $.map() and grep() functions into every array
Array.prototype.each = function (func) { $.each(this, func); }
Array.prototype.map = function (func) { return $.map(this, func); }
Array.prototype.find = function (func) { jQuery.grep(this, func); }

// A 'fold' function., e.g. [1, 2, 3].fold(add, 0) -> 6, assuming that add(a,b) is defined as a+b
Array.prototype.fold = function(combineFunc, tally) {
    if (this.isEmpty()) {
        return tally;
    }
    else {
        return this.rest().fold_iterative(
            combineFunc,
            combineFunc(this.first(),
                        tally));
    }
}

// Min and max
Array.prototype.min = function() {
	var min = 1e10;
	var my = this;
	$.each(this, function (x) { min = (my[x] < min) ? my[x] : min });
	return min;
}

Array.prototype.max = function() {
	var max = -1e10;
	var my = this;
	$.each(this, function (x) { max = (my[x] > max) ? my[x] : max });
	return max;
}

// ----------------------------------
// Add a test class method
jQuery.prototype.hasClass = function (name) {
	if (!name)
		return false;
	
	if (this.attr("class") == name)
		return true;
	else
		return false;
}

// ----------------------------------
// Maths
// Return a random number between m and n
Math.ran = function (m, n) {
	return m + Math.floor(Math.random(0)*(n-m+1));
}

// ----------------------------------
// A random generator from a list
function List(t) {
	return function () {
		return t.random();
	}
}

// ----------------------------------
// RGB functions

function RGBtoHex(R,G,B) {return "#"+toHex(R)+toHex(G)+toHex(B)}
function toHex(N) {
	if (N==null) return "00";
	N=parseInt(N); if (N==0 || isNaN(N)) return "00";
	N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
	return "0123456789ABCDEF".charAt((N-N%16)/16)
		+ "0123456789ABCDEF".charAt(N%16);
}

function rgbHexToArray (rgb) {
	if (rgb.slice(0,3) == 'rgb') {
		rgb = rgbStringToHex(rgb);
	}
	var red = parseInt(rgb.slice(1, 3), 16);
	var green = parseInt(rgb.slice(3, 5), 16);
	var blue = parseInt(rgb.slice(5, 7), 16);
	return [red, green, blue];
}

function rgbStringToHex(rgbColour) {
	try {
    	var rgbValues = rgbColour.substring(4, rgbColour.indexOf(")"));
		var rgbArray = rgbValues.split(", ");
		hexColour = RGBtoHex(rgbArray[0], rgbArray[1], rgbArray[2]);
	}
	catch(e) {
		alert("There was an error converting the RGB value " + rgbColour + " to Hexadecimal in function rgb2Hex");
	}
	return hexColour;
}

// The End
